package com.itheima01.day09test;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author 刘全
 * @date 2021/7/11
 */
public class StudentSystem {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        ArrayList<User> user = new ArrayList<>();
        User u = new User("lq", "0922");
        user.add(u);
        while (true) {
            System.out.println("请输入用户名:");
            String name = sc.nextLine();
            System.out.println("请输入密码:");
            String num = sc.nextLine();
            int index = -1;
            for (int i = 0; i < user.size(); i++) {
                if (user.get(i).getUserName().equals(name) && user.get(i).getPassWord().equals(num)) {
                    index = i;
                    break;
                }
            }
            if (index != -1) {
                System.out.println("登录成功");
                break;
            } else {
                System.out.println("用户名或密码错误,请重新输入");
            }
        }
        lo:
        while (true) {
            System.out.println("-------欢迎来到学生管理系统-------");
            System.out.println("1 添加学生信息");
            System.out.println("2 删除学生信息");
            System.out.println("3 修改学生信息");
            System.out.println("4 查看学生信息");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");
            int num = sc.nextInt();
            switch (num) {
                case 1:
                    list = add(list);
                    break;
                case 2:
                    list = remove(list);
                    break;
                case 3:
                    list = set(list);
                    break;
                case 4:
                    check(list);
                    break;
                case 5:
                    System.out.println("感谢您的使用!");
                    break lo;
                default:
                    System.out.println("您的选择不在范围内,请重新输入");
            }
        }
    }

    public static ArrayList<Student> add(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        String id = "";
        lo:
        while (true) {
            if (list.size() == 0) {
                System.out.println("请输入学生学号:");
                id = sc.next();
                break;
            } else {
                System.out.println("请输入学生学号:");
                id = sc.next();
                int index = -1;
                lo1:
                for (int i = 0; i < list.size(); i++) {
                    if (id.equals(list.get(i).getId())) {
                        index = i;
                        break lo1;
                    }
                }
                if (index != -1) {
                    System.out.println("该学号已存在,请重新添加!");
                } else {
                    break;
                }
            }
        }
        System.out.println("请输入学生姓名:");
        String name = sc.next();
        System.out.println("请输入学生年龄:");
        int age = sc.nextInt();
        System.out.println("请输入学生生日:年-月-日");
        String birthday = sc.next();
        Student stu = new Student(id, name, age, birthday);
        lo1:
        while (true) {
            if (list.size() == 0) {
                list.add(stu);
                System.out.println("添加成功!");
                break lo1;
            } else {
                for (int i = 0; i < list.size(); i++) {
                    list.add(stu);
                    System.out.println("添加成功!");
                    break lo1;
                }
            }
        }
        return list;
    }

    public static ArrayList<Student> remove(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        lo2:
        while (true) {
            if (list.size() == 0) {
                System.out.println("系统中暂无学生信息,请添加后再删除!");
                break;
            } else {
                System.out.println("请输入要删除的学号:");
                String id = sc.nextLine();
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getId().equals(id)) {
                        list.remove(list.get(i));
                        System.out.println("删除成功!");
                        break lo2;
                    } else {
                        if (i == list.size() - 1) {
                            System.out.println("该信息不存在,请重新输入!");
                            break;
                        }
                    }
                }
            }
        }
        return list;
    }

    public static ArrayList<Student> set(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        int index = -1;
        if (list.size() == 0) {
            System.out.println("系统中暂无学生信息,请添加后再修改!");
        } else {
            String id = "";
            String newId = "";
            lo:
            while (true) {
                System.out.println("请输入您要修改的学生学号:");
                id = sc.next();
                lo1:
                for (int i = 0; i < list.size(); i++) {
                    if (id.equals(list.get(i).getId())) {
                        index = i;
                        break lo1;
                    }
                }
                if (index != -1) {
                    while (true) {
                        int num = -1;
                        System.out.println("请输入新的学生学号:");
                        newId = sc.next();
                        for (int i = 0; i < list.size(); i++) {
                            if (newId.equals(list.get(i).getId())) {
                                num = i;
                                break;
                            }
                        }
                        if (num != -1) {
                            System.out.println("该学号已存在,请重新修改!");
                        } else {
                            break lo;
                        }
                    }
                    //System.out.println("该学号已存在,请重新修改!");
                } else {
                    System.out.println("该学号不存在,请重新输入!");
                }
            }
            System.out.println("请输入新的学生姓名:");
            String name = sc.next();
            System.out.println("请输入新的学生年龄:");
            int age = sc.nextInt();
            System.out.println("请输入新的学生生日:年-月-日");
            String birthday = sc.next();
            Student stu = new Student(newId, name, age, birthday);
            list.set(index, stu);
            System.out.println("修改成功!");
        }
        return list;
    }

    public static void check(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("无学生信息,请添加后再查看!");
        } else {
            System.out.println("学号" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "生日");
            for (int i = 0; i < list.size(); i++) {
                String id = list.get(i).getId();
                String name = list.get(i).getName();
                int age = list.get(i).getAge();
                String bir = list.get(i).getBirthday();
                System.out.println(id + "\t" + name + "\t" + age + "\t" + bir);
            }
        }
    }
}
